<%@ page contentType="text/html;charset=UTF-8" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>微电影管理系统</title>
    <meta content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no" name="viewport">
    <link rel="shortcut icon" href="${pageContext.request.contextPath}/resource/static/base/images/logo.png">
    <link rel="stylesheet"
          href="${pageContext.request.contextPath}/resource/static/admin/bootstrap/css/bootstrap.min.css">
    <link rel="stylesheet" href="${pageContext.request.contextPath}/resource/static/fonts/css/font-awesome.min.css">
    <link rel="stylesheet" href="${pageContext.request.contextPath}/resource/static/ionicons/css/ionicons.min.css">
    <link rel="stylesheet" href="${pageContext.request.contextPath}/resource/static/admin/dist/css/AdminLTE.min.css">
    <link rel="stylesheet"
          href="${pageContext.request.contextPath}/resource/static/admin/dist/css/skins/_all-skins.min.css">
    <link rel="stylesheet" type="text/css"
          href="${pageContext.request.contextPath}/resource/static/admin/plugins/datepicker/datepicker3.css">
    <style>
        * {
            font-family: "Microsoft YaHei";
        }

        .table > tbody > tr > td, .table > tbody > tr > th, .table > tfoot > tr > td, .table > tfoot > tr > th, .table > thead > tr > td, .table > thead > tr > th {
            vertical-align: middle;
            text-align: center;
        }

    </style>
</head>
<body class="hold-transition skin-blue sidebar-mini">
<div class="wrapper">
    <%@ include file="../5-public/AdminSidebar.jsp" %>

    <div class="content-wrapper">
        <!--内容-->
        <section class="content-header">
            <h1>微电影管理系统</h1>
            <ol class="breadcrumb">
                <li><a href="#"><i class="fa fa-dashboard"></i> 标签管理</a></li>
                <li class="active">标签列表</li>
            </ol>
        </section>
        <section class="content" id="showcontent">
            <div class="row">
                <div class="col-md-12">
                    <div class="box box-primary">
                        <div class="box-header">
                            <h3 class="box-title">标签列表</h3>
                            <div class="box-tools">
                                <div class="input-group input-group-sm" style="width: 150px;">
                                    <input type="text" id="table_search" name="table_search"
                                           class="form-control pull-right"
                                           placeholder="请输入关键字...">

                                    <div class="input-group-btn">
                                        <button type="submit" onclick="search()" class="btn btn-default"><i
                                                class="fa fa-search"></i>
                                        </button>
                                    </div>
                                </div>
                            </div>
                        </div>
                        <div class="box-body table-responsive no-padding">
                            <table class="table table-hover">
                                <tbody>
                                <tr>
                                    <th>编号</th>
                                    <th>名称</th>
                                    <th>添加时间</th>
                                    <th>操作事项</th>
                                </tr>
                                </tbody>
                            </table>
                        </div>
                        <div class="box-footer clearfix">
                            <ul class="pagination pagination-sm no-margin pull-right">
                                <li><a href="javascript:void(0)" onclick="changePage('firstPage')">首页</a></li>
                                <li><a href="javascript:void(0)" onclick="changePage('previousPage')">上一页</a></li>
                                <li><a href="javascript:void(0)" onclick="changePage('nextPage')">下一页</a></li>
                                <li><a href="javascript:void(0)" onclick="changePage('endPage')">尾页</a></li>
                            </ul>
                        </div>
                        <div id="pageControl"
                             pageNum="1"
                             pageSize="10"
                             hidden></div>


                    </div>
                </div>
            </div>
        </section>
        <!--内容-->
    </div>
    <footer class="main-footer">
        <div class="pull-right hidden-xs">
            <b>版本</b> 1.0
        </div>
        <strong>版权 &copy; 2017-2018 归<a href="">xxx</a>.</strong> 所有
    </footer>
    <div class="control-sidebar-bg"></div>
</div>
<script src="${pageContext.request.contextPath}/resource/static/admin/plugins/jQuery/jQuery-2.2.0.min.js"></script>
<script src="${pageContext.request.contextPath}/resource/static/admin/bootstrap/js/bootstrap.min.js"></script>
<script src="${pageContext.request.contextPath}/resource/static/admin/plugins/slimScroll/jquery.slimscroll.min.js"></script>
<script src="${pageContext.request.contextPath}/resource/static/admin/plugins/fastclick/fastclick.js"></script>
<script src="${pageContext.request.contextPath}/resource/static/admin/dist/js/app.min.js"></script>
<script src="${pageContext.request.contextPath}/resource/static/admin/dist/js/demo.js"></script>
<script src="//cdn.bootcss.com/holder/2.9.4/holder.min.js"></script>
<script src="${pageContext.request.contextPath}/resource/static/admin/plugins/datepicker/bootstrap-datepicker.js"></script>
<script src="${pageContext.request.contextPath}/resource/static/admin/plugins/datepicker/locales/bootstrap-datepicker.zh-CN.js"
        charset="UTF-8"></script>


</body>
<script>


    $(document).ready(changePage)

    function search() {
        let inputText = $("#table_search").val();
        if (inputText == '') {
            return
        }
        let fromData = {
            "inputText": inputText
        }
        $.ajax({
            url: "/admin/tag/searchTag",
            data: fromData,
            type: "post",
            dataType: "json",
            success: function (res) {
                let $tbody = $('tbody');
                let list = res.data;
                $tbody.empty();
                $tbody.append(
                    "<tr><th>编号</th><th>名称</th><th>添加时间</th><th>操作事项</th></tr>"
                )
                for (let i = 0; i < list.length; i++) {
                    $tbody.append(
                        "<tr><th>" + list[i].tagId + "</th><th>" + list[i].tagName + "</th><th>" + formatTime("YY/m/d HH时MM分SS秒", list[i].tagAddDate) + "</th>" +
                        " <td>" +

                        "<a class='label label-danger' href='javascript:void(0)' onclick=deleteTag(this)>删除</a></td>" +
                        "</tr>"
                    )
                }
            },
            error: function (e1, e2, e3) {
                console.log(e2);
            }
        })

    }

    /**
     * 翻页ajax
     * @param operation 检测按钮是什么
     */
    function changePage(operation) {

        let pageNum = $('#pageControl').attr("pageNum");
        let pageSize = $('#pageControl').attr("pageSize");

        pageSize = parseInt(pageSize);
        pageNum = parseInt(pageNum);

        //这里是匹配按钮
        {
            if (operation == 'firstPage') {
                pageNum = 0;
            }
            if (operation == 'endPage') {
                pageNum = pageSize;
            }
            if (operation == 'nextPage') {
                if (pageNum == pageSize) {
                    pageNum = pageSize;
                } else {
                    pageNum += 1;
                }

            }
            if (operation == 'previousPage') {
                if (pageNum <= 1) {
                    pageNum = 1;
                } else {
                    pageNum -= 1;
                }
            }
        }


        $('#pageControl').attr("pagenum", pageNum)

        $.ajax({
            url: "/admin/tag/changePage?pageNum=" + pageNum,
            type: "get",
            dataType: "json",
            success: function (res) {
                let $tbody = $('tbody');
                $tbody.empty();
                $tbody.append(
                    "<tr><th>编号</th><th>名称</th><th>添加时间</th><th>操作事项</th></tr>"
                )
                let list = res.data.list;
                for (let i = 0; i < list.length; i++) {
                    $tbody.append(
                        "<tr><th>" + list[i].tagId + "</th><th>" + list[i].tagName + "</th><th>" + formatTime("YY/m/d HH时MM分SS秒", list[i].tagAddDate) + "</th>" +
                        " <td>" +
                        "<a class='label label-danger' href='javascript:void(0)' onclick=deleteTag(this)>删除</a></td>" +
                        "</tr>"
                    )
                }
            },
            error: function (e1, e2, e3) {
                console.log(e2);
            }
        })
    }

    function deleteTag(obj) {
        let tagId = $(obj).parent().parent().children(":first").text();
        console.log(tagId)
        let statu = confirm("确定删除吗?");
        if (!statu) {
            return false;
        }
        $.ajax({
            url: "/admin/tag/deleteTag?tagId=" + tagId,
            type: "post",
            dataType: "json",
            success: function (res) {
                console.log(res);
                if (res.code == 200) {
                    changePage();
                }
            },
            error: function (e1, e2, e3) {
                console.log(e2);
            }
        })

    }

    const formatTime = (format = "", num = new Date().getTime()) => {
        format = format || "YYYY-mm-dd HH:MM:SS";   //第一个参数不填时，使用默认格式
        let ret, date, renum;
        // 处理时间戳，js一般获取的时间戳是13位，PHP一般是10位,根据实际情况做判断处理
        if (num.toString().length == 10) {
            date = new Date(parseInt(num) * 1000);
        } else {
            date = new Date(parseInt(num));
        }
        const opt = {
            "Y": date.getFullYear().toString(), // 年
            "m": (date.getMonth() + 1).toString(), // 月
            "d": date.getDate().toString(), // 日
            "H": date.getHours().toString(), // 时
            "M": date.getMinutes().toString(), // 分
            "S": date.getSeconds().toString() // 秒
            // 目前用的是这六种符号,有其他格式化字符需求可以继续添加，值必须转化成字符串
        };
        for (var k in opt) {
            ret = new RegExp("(" + k + "+)").exec(format);
            if (ret) {
                renum = (ret[1].length == 1) ? (opt[k]) : (opt[k].padStart(ret[1].length, "0")) //根据复数前面是否补零,如“mm”补零，单“m”前面不补零
                format = format.replace(ret[1], renum)  //替换
            }
            ;
        }
        ;
        return format;
    };
</script>
</html>
